import java.util.ArrayList;

class Solution {
    public List<List<String>> displayTable(List<List<String>> orders) {
        HashMap<Integer, HashMap<String, Integer>> table = new HashMap<>();
        HashSet<String> foods = new HashSet<String>();
        
        for (List<String> order: orders){
            HashMap<String, Integer> innerMap = table.get(Integer.valueOf(order.get(1)));
            if (innerMap == null){
                innerMap = new HashMap<String, Integer>();
            }
            if (innerMap.containsKey(order.get(2)) ) {
                innerMap.put(order.get(2), innerMap.get(order.get(2)) + 1);
            } else {
                innerMap.put(order.get(2), 1);
            }
            table.put(Integer.valueOf(order.get(1)) , innerMap);
            
            foods.add(order.get(2));
        }
        
        List<List<String>> res = new ArrayList<List<String>>();
        ArrayList<String> head = new ArrayList<String>();
        head.add("Table");
        for (String food: foods){
            head.add(food);
        }
        int len_foods = head.size() - 1;
        
        res.add(head);
        for(Map.Entry<Integer, HashMap<String, Integer>> ent: table.entrySet() ){
            ArrayList<String> row = new ArrayList<String>();
            row.add(String.valueOf(ent.getKey()));
            HashMap<String, Integer> valueMap = ent.getValue();
            for(int i=0; i< len_foods; ++i){
                String key = head.get(i+1);
                int value = valueMap.get(key);
                
                row.add(String.valueOf(value));
            }
            
            res.add(row);
        }
        
        return res;
    }
}
